--       _________ __                 __
--      /   _____//  |_____________ _/  |______     ____  __ __  ______
--      \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
--      /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \ 
--     /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
--             \/                  \/          \//_____/            \/ 
--  ______________________                           ______________________
--                        T H E   W A R   B E G I N S
--         Stratagus - A free fantasy real time strategy game engine
--
--	(c) Copyright 2013 by Kyran Jackson
--
--      This program is free software; you can redistribute it and/or modify
--      it under the terms of the GNU General Public License as published by
--      the Free Software Foundation; either version 2 of the License, or
--      (at your option) any later version.
--  
--      This program is distributed in the hope that it will be useful,
--      but WITHOUT ANY WARRANTY; without even the implied warranty of
--      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--      GNU General Public License for more details.
--  
--      You should have received a copy of the GNU General Public License
--      along with this program; if not, write to the Free Software
--      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
--

OldRunSinglePlayerGameMenu = RunSinglePlayerGameMenu

function CleanCustomGame_Lua()
	RunSinglePlayerGameMenu = OldRunSinglePlayerGameMenu

	GameDefinitionSetup("initialise")

	for i=0,15 do
		timers[i] = nil
		SetMapTeams(i, nil, nil)
		ftm_choice[i] = nil
		ftm_gold[i] = nil
		ftm_wood[i] = nil
		ftm_oil[i] = nil
		aiftm_index[i] = nil
		aiftm_terminate[i] = nil
		aiftm_loop[i] = nil
		aiftm_mana[i] = nil
		ftm_faction[i] = nil
		ftm_team[i] = nil
		ftm_team_tempx[i] = nil
		ftm_team_tempy[i] = nil
		ftm_team_x1[i] = nil
		ftm_team_y1[i] = nil
		ftm_team_x2[i] = nil
		ftm_team_y2[i] = nil
		ftm_team_startx[i] = nil
		ftm_team_starty[i] = nil
		ftm_team_orderx[i] = nil
		ftm_team_ordery[i] = nil
		for j = 0, 15 do
			aiftm_action[i][j] = nil
			aiftm_unit[i][j] = nil
			aiftm_quantity[i][j] = nil
			aiftm_x_to[i][j] = nil
			aiftm_y_to[i][j] = nil
			aiftm_x_from[i][j] = nil
			aiftm_y_from[i][j] = nil
			aiftm_unit_option1[i][j] = nil
			aiftm_unit_option2[i][j] = nil
			aiftm_unit2[i][j] = nil
			aiftm_unit2_option1[i][j] = nil
			aiftm_unit2_option2[i][j] = nil
			aiftm_unit3[i][j] = nil
			aiftm_unit3_option1[i][j] = nil
			aiftm_unit3_option2[i][j] = nil
		end
	end
	
	BlueLeader = nil
	UnitFootmanNum = nil
	UnitArcherNum = nil
	UnitRangerNum = nil
	UnitBallistaNum = nil
	UnitPaladinNum = nil
	UnitKnightNum = nil
	UnitMageNum = nil
	UnitDwarvesNum = nil
	
	Blue2Temp_x = nil
	Blue2Temp_y = nil
	Blue2Step = nil
	Blue2Mana = nil
	Blue1Temp_x = nil
	Blue1Temp_y = nil
	Blue1Step = nil
	Blue1Mana = nil
	BlueTemp = nil
	BlueTeam1 = nil
	BlueTeam2 = nil
	BlueTemp_x1 = nil
	BlueTemp_y1 = nil
	BlueTemp_x2 = nil
	BlueTemp_y2 = nil
	BlueTeam1_x1 = nil
	BlueTeam1_y1 = nil
	BlueTeam1_x2 = nil
	BlueTeam1_y2 = nil
	BlueTeam2_x1 = nil
	BlueTeam2_y1 = nil
	BlueTeam2_x2 = nil
	BlueTeam2_y2 = nil
	blueribbon_stepping = nil
	BlueBarracks1_x = nil
	BlueBarracks1_y = nil
	BlueBarracks2_x = nil
	BlueBarracks2_y = nil
	BlueBarracks3_x = nil
	BlueBarracks3_y = nil
	BlueBarracks4_x = nil
	BlueBarracks4_y = nil
	BlueInventor_x = nil
	BlueInventor_y = nil
	BlueMageTower_x = nil
	BlueMageTower_y = nil
	
	UnitGruntNum = nil
	UnitAxethrowerNum = nil
	UnitBerserkerNum = nil
	UnitCatapultNum = nil
	UnitOgreMageNum = nil
	UnitOgreNum = nil
	UnitDeathKnightNum = nil
	UnitGoblinSappersNum = nil
	
	redribbon_stepping = nil
	
	RedLeader = nil
	Red1Mana = nil
	Red1Mana = nil
	
	RedTemp = nil
	RedTemp_x1 = nil
	RedTemp_y1 = nil
	RedTemp_x2 = nil
	RedTemp_y2 = nil		
	Red2Temp_x = nil
	Red2Temp_y = nil
	Red2Step = nil
	Red2Mana = nil
	Red1Temp_x = nil
	Red1Temp_y = nil
	Red1Step = nil
	Red1Mana = nil
	RedTeam1 = nil
	RedTeam2 = nil
	RedTeam1_x1 = nil
	RedTeam1_y1 = nil
	RedTeam1_x2 = nil
	RedTeam1_y2 = nil
	RedTeam2_x1 = nil
	RedTeam2_y1 = nil
	RedTeam2_x2 = nil
	RedTeam2_y2 = nil
	
	RedBarracks1_x = nil
	RedBarracks1_y = nil
	RedBarracks2_x = nil
	RedBarracks2_y = nil
	RedBarracks3_x = nil
	RedBarracks3_y = nil
	RedBarracks4_x = nil
	RedBarracks4_y = nil
	RedInventor_x = nil
	RedInventor_y = nil
	RedMageTower_x = nil
	RedMageTower_y = nil
	
	-- Resetting the default map.
	mapname = "maps/skirmish/(2)timeless-isle.smp.gz"
	local buttonStatut = 0 -- 0:not initialised, 1: Ok, 2: Cancel
	mapinfo = {
		playertypes = {nil, nil, nil, nil, nil, nil, nil, nil},
		description = "",
		nplayers = 1,
		w = 32,
		h = 32,
		id = 0
}
-- Resetting the default map directory.
function RunSelectScenarioMenu()
  buttonStatut = 0
  local menu = WarMenu(nil, panel(5), false)
  menu:setSize(352, 352)
  menu:setPosition((Video.Width - 352) / 2, (Video.Height - 352) / 2)
  menu:setDrawMenusUnder(true)

  menu:addLabel(_("Select scenario"), 176, 8)

  local browser = menu:addBrowser("maps/skirmish", "^.*%.smp%.?g?z?$",
    24, 140, 300, 108, mapname)

  local l = menu:addLabel(browser:getSelectedItem(), 24, 260, Fonts["game"], false)

  local function cb(s)
    l:setCaption(browser:getSelectedItem())
    l:adjustSize()
  end
  browser:setActionCallback(cb)

  menu:addHalfButton("~!OK", "o", 48, 318,
    function()
      local cap = l:getCaption()

      if (browser:getSelected() < 0) then
        return
      end
      buttonStatut = 1
      mapname = browser.path .. cap
      menu:stop()
    end)
  menu:addHalfButton(_("~!Cancel"), "c", 198, 318,
    function() buttonStatut = 2; menu:stop() end)

  menu:run()
end
end

function StartCustomGame_Lua()
	RunSinglePlayerGameMenu = NewRunSinglePlayerGameMenu
	
	-- Selecting the default map.
	mapname = "maps/ftm/(4)beethoven-day.smp"
	local buttonStatut = 0 -- 0:not initialised, 1: Ok, 2: Cancel
	mapinfo = {
		playertypes = {nil, nil, nil, nil, nil, nil, nil, nil},
		description = "",
		nplayers = 1,
		w = 32,
		h = 32,
		id = 0
}
-- Selecting the map directory.
function RunSelectScenarioMenu()
  buttonStatut = 0
  local menu = WarMenu(nil, panel(5), false)
  menu:setSize(352, 352)
  menu:setPosition((Video.Width - 352) / 2, (Video.Height - 352) / 2)
  menu:setDrawMenusUnder(true)

  menu:addLabel(_("Select scenario"), 176, 8)

  local browser = menu:addBrowser("maps/ftm", "^.*%.smp%.?g?z?$",
    24, 140, 300, 108, mapname)

  local l = menu:addLabel(browser:getSelectedItem(), 24, 260, Fonts["game"], false)

  local function cb(s)
    l:setCaption(browser:getSelectedItem())
    l:adjustSize()
  end
  browser:setActionCallback(cb)

  menu:addHalfButton("~!OK", "o", 48, 318,
    function()
      local cap = l:getCaption()

      if (browser:getSelected() < 0) then
        return
      end
      buttonStatut = 1
      mapname = browser.path .. cap
      menu:stop()
    end)
  menu:addHalfButton(_("~!Cancel"), "c", 198, 318,
    function() buttonStatut = 2; menu:stop() end)
	
  menu:run()
end
end
  
function NewRunSinglePlayerGameMenu()
  local menu = WarMenu()
  local offx = (Video.Width - 640) / 2
  local offy = (Video.Height - 480) / 2
  local d
  local race
  local resources
  local opponents
  local numunits
  local gametype
  local mapl
  local descriptionl
  local tilesetdd

  if (wargus.tales == false) then
    menu:addLabel(wargus.Name .. " V" .. wargus.Version .. ", " .. wargus.Copyright, offx + 320, (Video.Height - 90) + 18*4, Fonts["small"]) -- Copyright information.
  end
  
  menu:addLabel(_("Scenario:"), offx + 16, offy + 360, Fonts["game"], false)
  mapl = menu:addLabel(string.sub(mapname, 6), offx + 16, offy + 360 + 24, Fonts["game"], false)
  descriptionl = menu:addLabel("descriptionl", offx + 16 + 70, offy + 360, Fonts["game"], false)

  menu:addLabel(_("~<For the Motherland~>"), (Video.Width / 2), offy + 192, nil, true)
  menu:addFullButton(_("S~!elect Scenario"), "e", offx + 640 - 224 - 16, offy + 360 + 36*0,
    function()
      local oldmapname = mapname
	  menu:stop()
      RunSelectScenarioMenu()
      if (mapname ~= oldmapname) then
		for player=0,15 do
			SetMapTeams(player, nil, nil)
		end
        GetMapInfo(mapname)
        MapChanged()
      end
	  NewRunSinglePlayerGameMenu()
    end)
  menu:addFullButton(_("~!Start Game"), "s", offx + 640 - 224 - 16, offy + 360 + 36*1,
    function()

	for player=0,15 do
		if (GameDefinition["Map"][player]["Team"] == 1) then
			if (GameDefinition["Map"][player]["Player"] == 1) then
				if (t1p1char:getSelected() == 1) then GameDefinition["Player"][1][1]["Name"] = "Aya Kalang" elseif (t1p1char:getSelected() == 2) then GameDefinition["Player"][1][1]["Name"] = "Shane Wolfe" 	elseif (t1p1char:getSelected() == 3) then GameDefinition["Player"][1][1]["Name"] = "Sandria Fields" end
				if (t1p1race:getSelected() == 1) then GameDefinition["Player"][1][1]["Race"] = "human" 		elseif (t1p1race:getSelected() == 2) then GameDefinition["Player"][1][1]["Race"] = "orc" 			end
			elseif (GameDefinition["Map"][player]["Player"] == 2) then
				if (t1p2char:getSelected() == 1) then GameDefinition["Player"][1][2]["Name"] = "Aya Kalang" elseif (t1p2char:getSelected() == 2) then GameDefinition["Player"][1][2]["Name"] = "Shane Wolfe" 	elseif (t1p1char:getSelected() == 3) then GameDefinition["Player"][1][2]["Name"] = "Sandria Fields" end
				if (t1p2race:getSelected() == 1) then GameDefinition["Player"][1][2]["Race"] = "human" 		elseif (t1p2race:getSelected() == 2) then GameDefinition["Player"][1][2]["Race"] = "orc" 			end
			elseif (GameDefinition["Map"][player]["Player"] == 3) then
				if (t1p3char:getSelected() == 1) then GameDefinition["Player"][1][3]["Name"] = "Aya Kalang" elseif (t1p3char:getSelected() == 2) then GameDefinition["Player"][1][3]["Name"] = "Shane Wolfe" 	elseif (t1p3char:getSelected() == 3) then GameDefinition["Player"][1][3]["Name"] = "Sandria Fields" end
				if (t1p3race:getSelected() == 1) then GameDefinition["Player"][1][3]["Race"] = "human" 		elseif (t1p3race:getSelected() == 2) then GameDefinition["Player"][1][3]["Race"] = "orc" 			end
			elseif (GameDefinition["Map"][player]["Player"] == 4) then
				if (t1p4char:getSelected() == 1) then GameDefinition["Player"][1][4]["Name"] = "Aya Kalang" elseif (t1p4char:getSelected() == 2) then GameDefinition["Player"][1][4]["Name"] = "Shane Wolfe" 	elseif (t1p4char:getSelected() == 3) then GameDefinition["Player"][1][4]["Name"] = "Sandria Fields" end
				if (t1p4race:getSelected() == 1) then GameDefinition["Player"][1][4]["Race"] = "human" 		elseif (t1p4race:getSelected() == 2) then GameDefinition["Player"][1][4]["Race"] = "orc" 			end
			end
		elseif (GameDefinition["Map"][player]["Team"] == 2) then
			if (GameDefinition["Map"][player]["Player"] == 1) then
				if (t2p1char:getSelected() == 1) then GameDefinition["Player"][2][1]["Name"] = "Aya Kalang" elseif (t2p1char:getSelected() == 2) then GameDefinition["Player"][2][1]["Name"] = "Shane Wolfe" 	elseif (t2p1char:getSelected() == 3) then GameDefinition["Player"][2][1]["Name"] = "Sandria Fields" end
				if (t2p1race:getSelected() == 1) then GameDefinition["Player"][2][1]["Race"] = "human" 		elseif (t2p1race:getSelected() == 2) then GameDefinition["Player"][2][1]["Race"] = "orc" 			end
			elseif (GameDefinition["Map"][player]["Player"] == 2) then
				if (t2p2char:getSelected() == 1) then GameDefinition["Player"][2][2]["Name"] = "Aya Kalang" elseif (t2p2char:getSelected() == 2) then GameDefinition["Player"][2][2]["Name"] = "Shane Wolfe" 	elseif (t2p1char:getSelected() == 3) then GameDefinition["Player"][2][2]["Name"] = "Sandria Fields" end
				if (t2p2race:getSelected() == 1) then GameDefinition["Player"][2][2]["Race"] = "human" 		elseif (t2p2race:getSelected() == 2) then GameDefinition["Player"][2][2]["Race"] = "orc" 			end
			elseif (GameDefinition["Map"][player]["Player"] == 3) then
				if (t2p3char:getSelected() == 1) then GameDefinition["Player"][2][3]["Name"] = "Aya Kalang" elseif (t1p3char:getSelected() == 2) then GameDefinition["Player"][1][3]["Name"] = "Shane Wolfe" 	elseif (t1p3char:getSelected() == 3) then GameDefinition["Player"][1][3]["Name"] = "Sandria Fields" end
				if (t2p3race:getSelected() == 1) then GameDefinition["Player"][2][3]["Race"] = "human" 		elseif (t1p3race:getSelected() == 2) then GameDefinition["Player"][1][3]["Race"] = "orc" 			end
			elseif (GameDefinition["Map"][player]["Player"] == 4) then
				if (t2p4char:getSelected() == 1) then GameDefinition["Player"][2][4]["Name"] = "Aya Kalang" elseif (t2p4char:getSelected() == 2) then GameDefinition["Player"][2][4]["Name"] = "Shane Wolfe" 	elseif (t2p4char:getSelected() == 3) then GameDefinition["Player"][2][4]["Name"] = "Sandria Fields" end
				if (t2p4race:getSelected() == 1) then GameDefinition["Player"][2][4]["Race"] = "human" 		elseif (t2p4race:getSelected() == 2) then GameDefinition["Player"][2][4]["Race"] = "orc" 			end
			end
		end
	end
      RunMap(mapname, nil, wc2.preferences.FogOfWar, false)
      menu:stop()
    end)
	--TODO: Cancel Game button doesn't work.
  menu:addFullButton(_("~!Cancel Game"), "c", offx + 640 - 224 - 16, offy + 360 + 36*2, function() menu:stop(1); RunSinglePlayerTypeMenu(); CleanCustomGame_Lua(); end)		
				
  local team1x = (Video.Width / 2) - 175
  local team1y = offy + 10 + 240
  local team2x = (Video.Width / 2) + 175
  local team2y = offy + 10 + 240
  local team3x = offx
  local team3y = offy + (10 + 240) - 20 + 400
  local team4x = offx + 40 + 300
  local team4y = offy + (10 + 240) - 20 + 400
  
  for player=0,15 do
	if (GameDefinition["Map"][player]["Team"] ~= nil) then
		if (GameDefinition["Map"][player]["Team"] == 1) then
			if (GameDefinition["Map"][player]["Player"] == 1) then
				menu:addLabel(_("~<Team 1~>"), team1x, team1y - 25, Fonts["game"], true)
				t1p1char = menu:addDropDown({_("Map Default"), _("Aya Kalang"), _("Shane Wolfe"), _("Sandria Fields")}, team1x - 152, team1y + 25*0,
					function(dd) end)
				t1p1char:setSize(152, 20)

				t1p1race = menu:addDropDown({_("Map Default"), _("Human"), _("Orc")}, team1x, team1y + 25*0,
					function(dd) end)
				t1p1race:setSize(152, 20)
			elseif (GameDefinition["Map"][player]["Player"] == 2) then
				t1p2char = menu:addDropDown({_("Map Default"), _("Aya Kalang"), _("Shane Wolfe"), _("Sandria Fields")}, team1x - 152, team1y + 25*1,
					function(dd) end)
				t1p2char:setSize(152, 20)
				  
				t1p2race = menu:addDropDown({_("Map Default"), _("Human"), _("Orc")}, team1x, team1y + 25*1,
					function(dd) end)
				t1p2race:setSize(152, 20)
			elseif (GameDefinition["Map"][player]["Player"] == 3) then
				menu:addLabel(_("~<Team 1~>"), team1x, team1y - 25, Fonts["game"], true)
				t1p3char = menu:addDropDown({_("Map Default"), _("Aya Kalang"), _("Shane Wolfe"), _("Sandria Fields")}, team1x - 152, team1y + 25*2,
					function(dd) end)
				t1p3char:setSize(152, 20)

				t1p3race = menu:addDropDown({_("Map Default"), _("Human"), _("Orc")}, team1x, team1y + 25*2,
					function(dd) end)
				t1p3race:setSize(152, 20)
			elseif (GameDefinition["Map"][player]["Player"] == 4) then
				t1p4char = menu:addDropDown({_("Map Default"), _("Aya Kalang"), _("Shane Wolfe"), _("Sandria Fields")}, team1x - 152, team1y + 25*3,
					function(dd) end)
				t1p4char:setSize(152, 20)
				  
				t1p4race = menu:addDropDown({_("Map Default"), _("Human"), _("Orc")}, team1x, team1y + 25*3,
					function(dd) end)
				t1p4race:setSize(152, 20)
			end
		elseif (GameDefinition["Map"][player]["Team"] == 2) then
			if (GameDefinition["Map"][player]["Player"] == 1) then
				menu:addLabel(_("~<Team 2~>"), team2x, team2y - 25, Fonts["game"], true)
				t2p1char = menu:addDropDown({_("Map Default"), _("Aya Kalang"), _("Shane Wolfe"), _("Sandria Fields")}, team2x - 152, team2y + 25*0,
					function(dd) end)
				t2p1char:setSize(152, 20)

				t2p1race = menu:addDropDown({_("Map Default"), _("Human"), _("Orc")}, team2x, team2y + 25*0,
					function(dd) end)
				t2p1race:setSize(152, 20)
			elseif (GameDefinition["Map"][player]["Player"] == 2) then
				t2p2char = menu:addDropDown({_("Map Default"), _("Aya Kalang"), _("Shane Wolfe"), _("Sandria Fields")}, team2x - 152, team2y + 25*1,
					function(dd) end)
				t2p2char:setSize(152, 20)
				  
				t2p2race = menu:addDropDown({_("Map Default"), _("Human"), _("Orc")}, team2x, team2y + 25*1,
					function(dd) end)
				t2p2race:setSize(152, 20)
			elseif (GameDefinition["Map"][player]["Player"] == 3) then
				menu:addLabel(_("~<Team 1~>"), team1x, team1y - 25, Fonts["game"], true)
				t2p3char = menu:addDropDown({_("Map Default"), _("Aya Kalang"), _("Shane Wolfe"), _("Sandria Fields")}, team2x - 152, team2y + 25*2,
					function(dd) end)
				t2p3char:setSize(152, 20)

				t2p3race = menu:addDropDown({_("Map Default"), _("Human"), _("Orc")}, team2x, team2y + 25*2,
					function(dd) end)
				t2p3race:setSize(152, 20)
			elseif (GameDefinition["Map"][player]["Player"] == 4) then
				t2p4char = menu:addDropDown({_("Map Default"), _("Aya Kalang"), _("Shane Wolfe"), _("Sandria Fields")}, team2x - 152, team2y + 25*3,
					function(dd) end)
				t2p4char:setSize(152, 20)
				  
				t2p4race = menu:addDropDown({_("Map Default"), _("Human"), _("Orc")}, team2x, team2y + 25*3,
					function(dd) end)
				t2p4race:setSize(152, 20)
			end
		end
	end
  end
  
  function MapChanged()
    mapl:setCaption(string.sub(mapname, 6))
    mapl:adjustSize()

    descriptionl:setCaption(mapinfo.description ..
      " (" .. mapinfo.w .. " x " .. mapinfo.h .. ")")
    descriptionl:adjustSize()

  end

  GetMapInfo(mapname)
  MapChanged()

  menu:run()
end
  
  
StartCustomGame_Lua()
  
RunSinglePlayerGameMenu()